package com.lais.record.base.controller;

import com.lais.record.base.constant.RecordBaseEnum;
import com.lais.record.base.pojo.Account;
import com.lais.record.base.service.AccountService;
import com.lais.record.common.constant.RecordEnum;
import com.lais.record.common.vo.PageResult;
import com.lais.record.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: luowen
 * @Description:
 * @Date: Create in 21:47 2019/11/27
 */
@RestController
@RequestMapping("account")
@Slf4j
@Api(value = "账户功能接口")
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 保存账户信息
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加账户", notes = "根据参数添加账户")
    public Result save(@RequestBody @Valid Account account , BindingResult result){
        log.info("账户添加--start...");
        log.info("账户添加--start...param:{}",account);
        if(result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            StringBuffer sb = new StringBuffer();
            fieldErrors.forEach(fieldError -> sb.append(fieldError.getDefaultMessage()).append(" "));
            return new Result("40000",sb.toString(),false);
        }
        try {
            accountService.save(account);
        }catch (Exception e){
            log.error("账户添加异常----exception:{},错误信息：{}",e,e.getMessage());
            return Result.fail();
        }
        log.info("账户添加--end...");
        return Result.success();
    }

    @PutMapping
    @ApiOperation(value = "修改账户", notes = "根据参数修改账户")
    public Result update(@RequestBody Account account){
        log.info("账户修改--start...");
        log.info("账户修改--start...param:{}",account);
        try {
            accountService.update(account);
        }catch (Exception e){
            log.error("账户修改异常----exception:{},错误信息：{}",e,e.getMessage());
            return Result.fail();
        }
        log.info("账户修改--end...");
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除账户", notes = "根据参数ids删除账户")
    public Result deleteByIds(List<String> ids){
        log.info("账户删除--start...");
        log.info("账户删除--start...param:{}",ids);
        try {
            accountService.deleteByIds(ids);
        }catch (Exception e){
            log.error("账户删除异常----exception:{},错误信息：{}",e,e.getMessage());
            return Result.fail();
        }
        log.info("账户删除--end...");
        return Result.success();
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询账户", notes = "根据根据id查询账户")
    public Result<Account> getById(@PathVariable("id")String id){
        log.info("账户查询--根据id--start...");
        log.info("账户查询--start...id:{}",id);
        try {
            Account account = accountService.getById(id);
            if(account == null){
                log.error("账户查询----账户信息不存在--id{}",id);
                return new Result(RecordBaseEnum.ACCOUNT_NOT_EXIST.getCode(),RecordBaseEnum.ACCOUNT_NOT_EXIST.getMessage(),false);
            }
            log.info("账户删除--end...");
            Result result = Result.success();
            result.setData(account);
            return result;
        }catch (Exception e){
            log.error("账户查询异常----exception:{},错误信息：{}",e,e.getMessage());
            return Result.fail();
        }
    }

    /**
     * 查询该用户下的所有账户信息
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询某用户下的所有账户", notes = "查询某用户下的所有账户")
    public Result<List<Account>> findAll(){
        log.info("账户查询所有--start...");
        try {
            List<Account> accounts = accountService.findAll();
            if(accounts == null || accounts.size()==0){
                log.error("账户查询所有----该用户不存在账户信息不存在--用户uid{}","");
                return new Result(RecordBaseEnum.ACCOUNT_NOT_EXIST.getCode(),RecordBaseEnum.ACCOUNT_NOT_EXIST.getMessage(),false);
            }
            Result result = Result.success();
            result.setData(accounts);
            log.info("账户查询所有--end...");
            return result;
        }catch (Exception e){
            log.error("账户查询异常----exception:{},错误信息：{}",e,e.getMessage());
            return Result.fail();
        }
    }

    @PostMapping("/{page}/{pageSize}")
    @ApiOperation(value = "分页查询所有账户", notes = "分页查询所有账户")
    public PageResult search(@ApiParam(value = "查询页码") @PathVariable("page")Integer page, @ApiParam(value = "查询条数") @PathVariable("pageSize")Integer pageSize, @ApiParam(value = "查询条件") @RequestBody(required = false) Account account){
        log.info("分页查询账户信息--start...");
        log.info("分页查询账户信息--start...page:{}--pageSize:{}--分页条件：{}",page,pageSize,account);
        PageResult result = accountService.search(page,pageSize,account);
        log.info("分页查询账户信息---end...--响应数据：{}",result);
        return result;
    }


}
